package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.guild.*;
    import mortal.game.view.guild.warehouse.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildWarehouseController extends Controller
    {
        private var _isWarehouseOpenByNpc:Boolean;
        private var _warehouseModule:GuildWarehouseModule;
        private var _contriWindow:ContriConfirmWindow;
        private var _applyListWindow:ApplyListWindow;
        private var _applyConfirmWindow:ApplyConfirmWindow;
        private var _replyListWindow:ReplyListWindow;
        private var _assignWindow:AssignItemWindow;
        private var _recordWindow:RecordListWindow;
        private var _assignConfirmWindow:AssignConfirmWindow;

        public function GuildWarehouseController()
        {
            Dispatcher.addEventListener(GuildEventName.WarehouseOpenApplyWindow, this.onWarehouseOpenApplyWindowHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseOpenReplyWindow, this.onWarehouseOpenReplyWindowHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseOpenAssignWindow, this.onWarehouseOpenAssignWindowHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseOpenPackWindow, this.onWarehouseOpenPackWindowHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseOpenRecordWindow, this.onWarehouseOpenRecordWindowHandler);
            Dispatcher.addEventListener(GuildEventName.SortMember, this.onSortMemberHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseOpenAssignNumWindow, this.onWarehouseOpenAssignNumWindowHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseItemDoubleClick, this.onWarehouseItemDoubleClickHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseOpen, this.onGuildWarehouseOpenHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseClose, this.onGuildWarehouseCloseHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetItems, this.onGuildWarehouseGetItemsHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetItemsSuccess, this.onGuildWarehouseGetItemsSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseOper, this.onGuildWarehouseOperHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseOperSuccess, this.onGuildWarehouseOperSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseDestroy, this.onGuildWarehouseDestroyHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseContri, this.onGuildWarehouseContriHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetRecords, this.onGuildWarehouseGetRecordsHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetRecordsSuccess, this.onGuildWarehouseGetRecordsSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseApply, this.onGuildWarehouseApplyHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseApplySuccess, this.onGuildWarehouseApplySuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseCancelApply, this.onGuildWarehouseCancelApplyHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseDraw, this.onGuildWarehouseDrawHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseAssign, this.onGuildWarehouseAssignHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseAssignSuccess, this.onGuildWarehouseAssignSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseReply, this.onGuildWarehouseReplyHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseReplySuccess, this.onGuildWarehouseReplySuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetAllApply, this.onGuildWarehouseGetAllApplyHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetAllApplySuccess, this.onGuildWarehouseGetAllApplySuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetSelfApply, this.onGuildWarehouseGetSelfApplyHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseGetSelfApplySuccess, this.onGuildWarehouseGetSelfApplySuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseTake, this.onGuildWarehouseTakeHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGirdWalkEnd);
            
        }

        public function init() : void
        {
            
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        override protected function initView() : IView
        {
            if (this._warehouseModule == null)
            {
                this._warehouseModule = new GuildWarehouseModule();
                this._warehouseModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._warehouseModule;
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            NpcEffectRule.registDialogEffectWindow(this.view);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseGetItems));
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildWarehouseUpdateCommand, this.onGuildWarehouseUpdateCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarehouseApplyUpdateCommand, this.onGuildWarehouseApplyUpdateCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildUpdateInfo, this.onGuildUpdateInfoHandler);
            
        }

        private function onGuildWarehouseUpdateCommandHandler(param1:Array) : void
        {
            if (param1)
            {
                if (this._warehouseModule)
                {
                    this._warehouseModule.setDefaultPage(param1);
                }
            }
            
        }

        private function onGuildWarehouseApplyUpdateCommandHandler(param1:Array) : void
        {
            if (param1)
            {
                if (this._applyListWindow && this._applyListWindow.isHide == false)
                {
                    this._applyListWindow.setDefaultPage(param1);
                }
            }
            
        }

        public function isShow() : Boolean
        {
            return this._warehouseModule && this._warehouseModule.isHide == false;
        }

        private function onWarehouseOpenApplyWindowHandler(event:DataEvent) : void
        {
            if (this._applyListWindow == null)
            {
                this._applyListWindow = new ApplyListWindow();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseGetSelfApply));
            this.onGuildWarehouseCloseHandler(null);
            this._applyListWindow.show(825, this._warehouseModule.y);
            
        }

        private function onWarehouseOpenAssignNumWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["itemData"] as ItemData;
            var _loc_3:* = event.data["posIndex"] as int;
            var _loc_4:* = new ItemData(_loc_2.itemCode);
            new ItemData(_loc_2.itemCode).data.uid = _loc_2.uid;
            _loc_4.data.itemAmount = _loc_2.itemAmount;
            _loc_4.data.itemCode = _loc_2.itemCode;
            if (this._assignConfirmWindow == null)
            {
                this._assignConfirmWindow = new AssignConfirmWindow();
            }
            this._assignConfirmWindow.toIndex = _loc_3;
            this._assignConfirmWindow.updateByItemData(_loc_4);
            this._assignConfirmWindow.show();
            
        }

        private function onWarehouseOpenReplyWindowHandler(event:DataEvent) : void
        {
            if (this._replyListWindow == null)
            {
                this._replyListWindow = new ReplyListWindow();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseGetAllApply));
            this.onGuildWarehouseCloseHandler(null);
            this._replyListWindow.show(825, this._warehouseModule.y);
            
        }

        private function onWarehouseOpenAssignWindowHandler(event:DataEvent) : void
        {
            if (this._assignWindow == null)
            {
                this._assignWindow = new AssignItemWindow();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetPlayerList, cache.guild.myGuildInfo.guildId));
            this.onGuildWarehouseCloseHandler(null);
            this._assignWindow.show(825, this._warehouseModule.y);
            
        }

        private function onWarehouseOpenPackWindowHandler(event:DataEvent) : void
        {
            GameController.pack.view.show(815, this._warehouseModule.y);
            
        }

        private function onWarehouseOpenRecordWindowHandler(event:DataEvent) : void
        {
            if (this._recordWindow == null)
            {
                this._recordWindow = new RecordListWindow();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseGetRecords, cache.guild.myGuildInfo.guildId));
            this.onGuildWarehouseCloseHandler(null);
            this._recordWindow.show(825, this._warehouseModule.y);
            
        }

        private function onSortMemberHandler(event:DataEvent) : void
        {
            var _loc_2:String = null;
            var _loc_3:Array = null;
            if (this._assignWindow && this._assignWindow.isHide == false)
            {
                _loc_2 = event.data as String;
                _loc_3 = cache.guild.getSortedMemberArray(_loc_2);
                this._assignWindow.setDefaultPage(_loc_3);
            }
            
        }

        private function onGuildWarehouseOpenHandler(event:DataEvent) : void
        {
            if (cache.guild.getWarehouseLevel() < 1)
            {
                MsgManager.addBroadCast("仙盟仓库1级才能开启，请先升级！");
                
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CloseGuildWindow));
            var _loc_2:* = (Global.stage.stageHeight - GameController.pack.view.height) / 2;
            this.view.show(380, _loc_2);
            GameController.pack.view.show(825, _loc_2);
            
        }

        private function onGuildWarehouseCloseHandler(event:DataEvent) : void
        {
            if (this._replyListWindow)
            {
                this._replyListWindow.hide();
            }
            if (this._assignWindow)
            {
                this._assignWindow.hide();
            }
            if (this._recordWindow)
            {
                this._recordWindow.hide();
            }
            if (this._applyListWindow)
            {
                this._applyListWindow.hide();
            }
            if (this._contriWindow)
            {
                this._contriWindow.hide();
            }
            if (this._applyConfirmWindow)
            {
                this._applyConfirmWindow.hide();
            }
            if (this._assignConfirmWindow)
            {
                this._assignConfirmWindow.hide();
            }
            GameController.pack.view.hide();
            
        }

        private function onGuildWarehouseGetItemsHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getGuildWarehouses();
            
        }

        private function onGuildWarehouseGetItemsSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            this._warehouseModule.setDefaultPage(_loc_2);
            
        }

        private function onGuildWarehouseOperHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["oper"] as EGuildWarehouseOper;
            var _loc_3:* = event.data["item"] as SPlayerItem;
            GameProxy.guildProxy.operGuildWarehouse(_loc_2, _loc_3);
            
        }

        private function onGuildWarehouseContriHandler(event:DataEvent) : void
        {
            var _loc_2:* = EGuildWarehouseOper.EGuildWarehouseIn;
            var _loc_3:* = event.data as ItemData;
            if (_loc_3)
            {
                this.onGuildWarehouseCloseHandler(null);
                if (this._contriWindow == null)
                {
                    this._contriWindow = new ContriConfirmWindow();
                }
                this._contriWindow.updateByItemData(_loc_3);
                this._contriWindow.show();
                if (this._warehouseModule)
                {
                    this._contriWindow.x = this._warehouseModule.x + 70;
                    this._contriWindow.y = this._warehouseModule.y + 95;
                }
                this.onWarehouseOpenPackWindowHandler(null);
            }
            
        }

        private function onGuildWarehouseOperSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as EGuildWarehouseOper;
            if (_loc_2 == EGuildWarehouseOper.EGuildWarehouseIn)
            {
                MsgManager.addBroadCast("捐献物品成功");
            }
            else
            {
                MsgManager.addBroadCast("取出物品成功");
            }
            
        }

        private function onGuildWarehouseDestroyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.guildProxy.destroyGuildWarehouse(_loc_2);
            
        }

        private function onGuildWarehouseGetRecordsHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getGuildWarehouseRecords();
            
        }

        private function onGuildWarehouseGetRecordsSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            cache.guild.warehouseRecodsArray = _loc_2;
            if (this._recordWindow && this._recordWindow.isHide == false)
            {
                this._recordWindow.setDefaultPage(cache.guild.warehouseRecodsArray);
            }
            
        }

        private function onGuildWarehouseApplyHandler(event:DataEvent) : void
        {
            var _loc_3:String = null;
            var _loc_4:int = 0;
            var _loc_5:ItemData = null;
            var _loc_2:* = event.data as SPlayerItem;
            if (_loc_2)
            {
                if (_loc_2.itemAmount > 1)
                {
                    _loc_5 = new ItemData(_loc_2.itemCode);
                    _loc_5.data.uid = _loc_2.uid;
                    _loc_5.itemAmount = _loc_2.itemAmount;
                    if (this._applyConfirmWindow == null)
                    {
                        this._applyConfirmWindow = new ApplyConfirmWindow();
                    }
                    this._applyConfirmWindow.updateByItemData(_loc_5);
                    this._applyConfirmWindow.show();
                }
                else
                {
                    if (this._applyConfirmWindow && this._applyConfirmWindow.isHide == false)
                    {
                        this._applyConfirmWindow.hide();
                    }
                    _loc_3 = _loc_2.uid;
                    _loc_4 = _loc_2.itemAmount;
                    GameProxy.guildProxy.applyGuildWarehouse(_loc_3, _loc_4);
                }
            }
            
        }

        private function onGuildWarehouseApplySuccessHandler(event:DataEvent) : void
        {
            MsgManager.addBroadCast("申请已发送成功");
            
        }

        private function onGuildWarehouseCancelApplyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.guildProxy.cancelApplyGuildWarehouse(_loc_2);
            
        }

        private function onGuildWarehouseDrawHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.guildProxy.drawGuildWarehouse(_loc_2);
            
        }

        private function onGuildWarehouseAssignHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["toPlayerId"] as int;
            var _loc_3:* = event.data["itemMap"] as Dictionary;
            GameProxy.guildProxy.assignGuildWarehouse(_loc_2, _loc_3);
            
        }

        private function onGuildWarehouseAssignSuccessHandler(event:DataEvent) : void
        {
            this._assignWindow.resetToDefault();
            MsgManager.addBroadCast("分配物品成功");
            
        }

        private function onGuildWarehouseReplyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["toPlayerId"] as int;
            var _loc_3:* = event.data["applyInfo"] as SGuildWarehouseApply;
            var _loc_4:* = event.data["result"] as Boolean;
            GameProxy.guildProxy.replyGuildWarehouse(_loc_2, _loc_3, _loc_4);
            
        }

        private function onGuildWarehouseReplySuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["applyInfo"] as SGuildWarehouseApply;
            var _loc_3:* = event.data["result"] as Boolean;
            if (_loc_3)
            {
                MsgManager.addBroadCast("你已批准了申请物品请求。");
            }
            else
            {
                MsgManager.addBroadCast("你已拒绝了申请物品请求。");
            }
            this._replyListWindow.removeReplyedApply(_loc_2);
            
        }

        private function onGuildWarehouseGetAllApplyHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getAllGuildWarehouseApplys();
            
        }

        private function onGuildWarehouseGetAllApplySuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            if (this._replyListWindow && this._replyListWindow.isHide == false)
            {
                this._replyListWindow.setDefaultPage(_loc_2);
            }
            
        }

        private function onGuildWarehouseGetSelfApplyHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getGuildWarehouseApplys();
            
        }

        private function onGuildWarehouseGetSelfApplySuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            if (this._applyListWindow && this._applyListWindow.isHide == false)
            {
                this._applyListWindow.setDefaultPage(_loc_2);
            }
            
        }

        private function onGuildWarehouseTakeHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPlayerItem;
            if (_loc_2)
            {
                if (!cache.guild.isCanManageWarehouse())
                {
                    MsgManager.addBroadCast("你没有权限进行物品操作！");
                    
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseOper, {oper:EGuildWarehouseOper.EGuildWarehouseOut, item:_loc_2}));
            }
            
        }

        private function onGirdWalkEnd(event:PlayerEvent) : void
        {
            var _loc_2:AStarPoint = null;
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_5:Number = NaN;
            if (this.isWarehouseOpenByNpc)
            {
                _loc_2 = RolePlayer.instance.currentPoint;
                if (this._warehouseModule && this._warehouseModule.isHide == false)
                {
                    _loc_3 = cache.dialog.npcInfo;
                    if (_loc_3)
                    {
                        _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x, _loc_3.snpc.point.y);
                        _loc_5 = Math.sqrt(Math.pow(_loc_4.x - _loc_2.x, 2) + Math.pow(_loc_4.y - _loc_2.y, 2));
                        if (_loc_5 > 120)
                        {
                            this._warehouseModule.hide();
                            this.onGuildWarehouseCloseHandler(null);
                        }
                    }
                }
            }
            
        }

        private function onGuildUpdateInfoHandler(param1:SPlayerGuildInfo) : void
        {
            if (this._warehouseModule)
            {
                this._warehouseModule.updateRightMenu();
                this._warehouseModule.updateOperMenu();
            }
            
        }

        private function onWarehouseItemDoubleClickHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPlayerItem;
            if (_loc_2)
            {
                if ((this._applyListWindow == null || this._applyListWindow.isHide == true) && (this._assignWindow == null || this._assignWindow.isHide == true))
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseTake, _loc_2));
                }
                else if (this._applyListWindow && this._applyListWindow.isHide == false)
                {
                    Dispatcher.dispatchEvent(new DataEvent(GuildEventName.WarehouseSendToApply, _loc_2));
                }
                else if (this._assignWindow && this._assignWindow.isHide == false)
                {
                    Dispatcher.dispatchEvent(new DataEvent(GuildEventName.WarehouseSendToAssign, _loc_2));
                }
            }
            
        }

        public function get isWarehouseOpenByNpc() : Boolean
        {
            return this._isWarehouseOpenByNpc;
        }

        public function set isWarehouseOpenByNpc(param1:Boolean) : void
        {
            this._isWarehouseOpenByNpc = param1;
            
        }

    }
}
